package com.siwei.mes.job;

import com.siwei.mes.entity.experiment.CheckBatchConfig;
import com.siwei.mes.entity.experiment.CheckQuickConfig;
import com.siwei.mes.entity.system.TestProjectInfo;
import com.siwei.mes.enums.CheckTypeEnum;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.enums.TestProjectEnum;
import com.siwei.mes.mapper.experiment.CheckBatchConfigMapper;
import com.siwei.mes.mapper.experiment.CheckQuickConfigMapper;
import com.siwei.mes.mapper.system.TestProjectInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 生成混凝土试验委托  按照月检、季检、半年检、年检   2024-08-18 17:01:01  混凝土检测类型 不考虑  以后也不会有 都是常规
 *
 * @Description: 生成混凝土试验委托  月检、季检、半年检、年检
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/8/18 15:28
 */
@Component
public class GenConcreteExperimentByCheckTypeJob {
    private static final Logger log = LoggerFactory.getLogger("job");
    @Resource
    private CheckQuickConfigMapper checkQuickConfigMapper;
    @Resource
    private CheckBatchConfigMapper checkBatchConfigMapper;
    @Resource
    private TestProjectInfoMapper testProjectInfoMapper;

    /**
     * 获取所有快检指标配置
     *
     * @param checkType 检测类型 0-常规 10-月检  20-季检  30-半年检  40-年检
     * @return
     */
    public List<CheckQuickConfig> getQuickConfigList(Integer checkType) {
        CheckQuickConfig quickConfig = new CheckQuickConfig();
        quickConfig.setProjectCategory(MaterialsEnum.CONCRETE.getCode());
        quickConfig.setCheckType(checkType);
        return checkQuickConfigMapper.getList(quickConfig);
    }

    /**
     * 获取所有批检指标配置  混凝土这里批检只有抗压强度
     *
     * @return
     */
    public List<CheckBatchConfig> getBatchConfig(Integer checkType) {
        CheckBatchConfig batchConfig = new CheckBatchConfig();
        batchConfig.setProjectCategory(MaterialsEnum.CONCRETE.getCode());
        batchConfig.setCheckType(checkType);
        batchConfig.setTestProjectCode(TestProjectEnum.CONCRETE_PARAM_KYQD.getCode());
        return checkBatchConfigMapper.getList(batchConfig);
    }

    /**
     * 每个月1号生成月检的
     */
    //@Scheduled(cron = " 0 1 1 * * *")
    public void genMonth() {
        CheckTypeEnum checkTypeEnum = CheckTypeEnum.MONTH_CHECK;
        log.info("【混凝土】{}定时任务执行开始！", checkTypeEnum.getName());
        /**这里是快检的*/
        List<CheckQuickConfig> quickConfigList = this.getQuickConfigList(checkTypeEnum.getCode());
        if (quickConfigList.isEmpty()) {
            log.info("【混凝土快检】暂无需要生成试验委托！快检指标配置中没有【{}】的项目", checkTypeEnum.getName());
        } else {
            /**新增混凝土快检试验委托*/
            this.createQuickConcreteExperiment(checkTypeEnum, quickConfigList);
        }
        /**这里是批检的*/
        List<CheckBatchConfig> batchConfigList = this.getBatchConfig(checkTypeEnum.getCode());
        if (batchConfigList.isEmpty()) {
            log.info("【混凝土批检】暂无需要生成试验委托！批检指标配置中没有【{}】的项目", checkTypeEnum.getName());
        } else {
            /**新增混凝土批检试验委托*/
            this.createBatchConcreteExperiment(checkTypeEnum, batchConfigList);
        }
        log.info("【混凝土】{}定时任务执行完毕！", checkTypeEnum.getName());
    }

    /**
     * 新增混凝土批检试验委托
     *
     * @param checkTypeEnum
     * @param batchConfigList
     */
    private void createBatchConcreteExperiment(CheckTypeEnum checkTypeEnum, List<CheckBatchConfig> batchConfigList) {
        try {

        } catch (Exception e) {
            log.error("【混凝土快检】新增试验委托失败，检测类型：【{}】,失败原因：【{}】", checkTypeEnum.getName(), e.getMessage(), e);
        }
    }

    /**
     * 新增混凝土批检试验委托
     *
     * @param checkTypeEnum   检测类型枚举
     * @param quickConfigList 快检指标配置集合
     */
    private void createQuickConcreteExperiment(CheckTypeEnum checkTypeEnum, List<CheckQuickConfig> quickConfigList) {
        try {
            /**委托试验名称*/
            Set<String> entrustExperimentSet = new HashSet<>();
            /**试验项目code集合*/
            Set<String> testProjectCodeList = new HashSet<>();

            for (CheckQuickConfig quickConfig : quickConfigList) {
                /**放入试验项目code集合*/
                List<String> codeList = Arrays.asList(quickConfig.getTestProjectCode().split(","));
                testProjectCodeList.addAll(codeList);
                /**放入委托试验名称*/
                List<String> nameList = Arrays.asList(quickConfig.getTestProjectName().split(","));
                entrustExperimentSet.addAll(nameList);
            }
            /**获取所有试验项目配置*/
            List<TestProjectInfo> testProjectInfoList = testProjectInfoMapper.getList(new TestProjectInfo().setTestType(MaterialsEnum.CONCRETE.getTestProjectType()).setTestCodeList(testProjectCodeList));

            String entrustExperiment = null;
            if (!entrustExperimentSet.isEmpty()) {
                entrustExperiment = entrustExperimentSet.stream().map(String::valueOf).collect(Collectors.joining(","));
            }
        } catch (Exception e) {
            log.error("【混凝土快检】新增试验委托失败，检测类型：【{}】,失败原因：【{}】", checkTypeEnum.getName(), e.getMessage(), e);
        }
    }

    /**
     * 1,4,7,10 的1号生成季检
     */
    //@Scheduled(cron = " 0 1 1 1,4,7,10 * *")
    public void genQuarter() {
        CheckTypeEnum checkTypeEnum = CheckTypeEnum.QUARTER_CHECK;
        log.info("【混凝土】{}定时任务执行开始！", checkTypeEnum.getName());
        /**这里是快检的*/
        List<CheckQuickConfig> quickConfigList = this.getQuickConfigList(checkTypeEnum.getCode());
        if (quickConfigList.isEmpty()) {
            log.info("【混凝土快检】暂无需要生成试验委托！快检指标配置中没有【{}】的项目", checkTypeEnum.getName());
        } else {
            /**新增混凝土快检试验委托*/
            this.createQuickConcreteExperiment(checkTypeEnum, quickConfigList);
        }
        /**这里是批检的*/
        List<CheckBatchConfig> batchConfigList = this.getBatchConfig(checkTypeEnum.getCode());
        if (batchConfigList.isEmpty()) {
            log.info("【混凝土批检】暂无需要生成试验委托！批检指标配置中没有【{}】的项目", checkTypeEnum.getName());
        } else {
            /**新增混凝土批检试验委托*/
            this.createBatchConcreteExperiment(checkTypeEnum, batchConfigList);
        }
        log.info("【混凝土】{}定时任务执行完毕！", checkTypeEnum.getName());
    }

    /**
     * 1,7 的1号生成半年检
     */
    //@Scheduled(cron = " 0 1 1 1,7 * *")
    public void genHalfYear() {
        CheckTypeEnum checkTypeEnum = CheckTypeEnum.HALF_YEAR_CHECK;
        log.info("【混凝土】{}定时任务执行开始！", checkTypeEnum.getName());
        /**这里是快检的*/
        List<CheckQuickConfig> quickConfigList = this.getQuickConfigList(checkTypeEnum.getCode());
        if (quickConfigList.isEmpty()) {
            log.info("【混凝土快检】暂无需要生成试验委托！快检指标配置中没有【{}】的项目", checkTypeEnum.getName());
        } else {
            /**新增混凝土快检试验委托*/
            this.createQuickConcreteExperiment(checkTypeEnum, quickConfigList);
        }
        /**这里是批检的*/
        List<CheckBatchConfig> batchConfigList = this.getBatchConfig(checkTypeEnum.getCode());
        if (batchConfigList.isEmpty()) {
            log.info("【混凝土批检】暂无需要生成试验委托！批检指标配置中没有【{}】的项目", checkTypeEnum.getName());
        } else {
            /**新增混凝土批检试验委托*/
            this.createBatchConcreteExperiment(checkTypeEnum, batchConfigList);
        }
        log.info("【混凝土】{}定时任务执行完毕！", checkTypeEnum.getName());
    }

    /**
     * 年检  每年的1月1号凌晨1点执行
     */
    //@Scheduled(cron = " 0 1 1 1 * *")
    public void genYear() {
        CheckTypeEnum checkTypeEnum = CheckTypeEnum.YEAR_CHECK;
        log.info("【混凝土】{}定时任务执行开始！", checkTypeEnum.getName());
        /**这里是快检的*/
        List<CheckQuickConfig> quickConfigList = this.getQuickConfigList(checkTypeEnum.getCode());
        if (quickConfigList.isEmpty()) {
            log.info("【混凝土快检】暂无需要生成试验委托！快检指标配置中没有【{}】的项目", checkTypeEnum.getName());
        } else {
            /**新增混凝土快检试验委托*/
            this.createQuickConcreteExperiment(checkTypeEnum, quickConfigList);
        }
        /**这里是批检的*/
        List<CheckBatchConfig> batchConfigList = this.getBatchConfig(checkTypeEnum.getCode());
        if (batchConfigList.isEmpty()) {
            log.info("【混凝土批检】暂无需要生成试验委托！批检指标配置中没有【{}】的项目", checkTypeEnum.getName());
        } else {
            /**新增混凝土批检试验委托*/
            this.createBatchConcreteExperiment(checkTypeEnum, batchConfigList);
        }
        log.info("【混凝土】{}定时任务执行完毕！", checkTypeEnum.getName());
    }
}
